﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using lipesin.Main.Category;

namespace lipesin.Main
{
	public partial class Main : Form
	{
		//Dalc _dal = new Dalc();
		private DataTable _category0;
		private DataTable _category1;
		private DataTable _category2;

		public Main()
		{
			InitializeComponent();
		}

		private void Main_Load(object sender, EventArgs e)
		{
			string tmp = String.Format("a.[{0}]=N'{0:yyyyMMdd}'", DateTime.Now);

			//bool match = Regex.IsMatch("Z", @"^([\dа-яА-Яa-zA-Z]([\dа-яА-Яa-zA-Z\s-/])*)*$");
			//ExportDogsFormat.CreateTestFile();

			//this.Close(); return;
			//dtpFrom.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
			//dtpTo.Value = Convert.ToDateTime(dtpFrom.Value).AddMonths(1);
			//RefreshLists();


			//DataRow row = test.NewRow();
			//test.Rows.Add(row);
			//row["name"] = "test2";
			//row["type"] = 0;
			//row["id"] = dal.NextId();
			//dal.UpdateDataTable(test);

		}

		private void RefreshLists()
		{
			using (SqlCommand cmd = new SqlCommand())
			{
				cmd.Parameters.AddWithValue("@date_from", dtpFrom.Value);
				cmd.Parameters.AddWithValue("@date_to", dtpTo.Value);

				cmd.CommandText =
					@"	select * from dbo.category 
						where
							type = 0
							and date_from >= @date_from and (date_to is null or date_to <= @date_to)";
				_category0 = Dalc.ExecuteToDataTable(cmd);

				cmd.CommandText =
					@"	select id, name, parent
						from dbo.category
						where
							type = 2 or not parent is null
							and date_from >= @date_from and (date_to is null or date_to <= @date_to)";
				_category2 = Dalc.ExecuteToDataTable(cmd);
			}

			grd0.DataSource = _category0;
			DataSet set = new DataSet();
			_category2.TableName = "cat2";
			set.Tables.Add(_category2);
			set.Relations.Add("recursive", _category2.Columns["id"], _category2.Columns["parent"]);
			grd2.DataSource = _category2;
		}

		private void dtpFrom_ValueChanged(object sender, EventArgs e)
		{
			dtpTo.Value = Convert.ToDateTime(dtpFrom.Value).AddMonths(1);
		}

		private void btnRefresh_Click(object sender, EventArgs e)
		{
			RefreshLists();
		}

		private void grd2_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
		{
			//foreach (UltraGridColumn col in e.Layout.Bands[0].Columns) col.visi
			foreach (var band in e.Layout.Bands)
			{
				band.Columns["id"].Hidden = true;
				band.ColHeadersVisible = false;
				//band.Columns["name"].Width = band.Columns["name"].CalculateAutoResizeWidth(PerformAutoSizeType.AllRowsInBand, true);
			}
			e.Layout.Bands[0].ColHeadersVisible = true;
		}

		private void grd2_InitializeRow(object sender, InitializeRowEventArgs e)
		{
			DataRow row = ((DataRowView)e.Row.ListObject).Row;
			if (!Convert.IsDBNull(row["parent"]) && e.Row.ParentRow == null) e.Row.Hidden = true;
		}

		private void grd0_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Copy;
		}

		private void grd1_DragOver(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Copy;
			
		}


		private void Test()
		{

		}

		private void ctlAction_Click(object sender, EventArgs e)
		{
			CategorySelector selector = new CategorySelector();
			selector.Init(CategoryType.Expence, 12);
			selector.ShowDialog(this);
		}
	}
}
